<template>
  <page-header-wrapper>
    <a-card :bordered="false" class="search-card m-b-20">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="14" :lg="12" :xl="8" :sm="24">
              <a-form-item label="菜单名称">
                <a-input v-model="queryParam.Title" placeholder="请输入菜单名称" @pressEnter="$refs.table.refresh(true)" />
              </a-form-item>
            </a-col>
            <a-col :md="10" :lg="8" :xl="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        class="menu-table"
        ref="table"
        size="default"
        :scroll="{x: true}"
        :rowKey="record => record.ID"
        :columns="columns"
        :data="loadData"
        :pagination="{ showTotal: total => `共 ${total} 条` }"
      >
        <!-- 表格操作栏 -->
        <template slot="tabAction">
          <a-button type="primary" icon="plus" @click="handleMenu('add-menu')">新增菜单</a-button>
        </template>
        <span slot="menuTitle" slot-scope="text">
          <ellipsis :length="30" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="menuType" slot-scope="record">
          <a-tag :color="record.MenuUrl === 'RouteView' ? 'cyan' : 'blue'">{{ record.MenuUrl === 'RouteView' ? '目录' : '菜单' }}</a-tag>
        </span>
        <span slot="icon" slot-scope="text">
          <a-icon v-if="text!==''" :type="text"></a-icon>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleMenu('add-sonMenu', record)">新增子菜单</a>
            <a-divider type="vertical" />
            <a @click="handleMenu('edit-menu', record)">修改</a>
            <a-divider type="vertical" />
            <a-popconfirm
              title="您确定是否删除当前菜单?"
              ok-text="是"
              cancel-text="否"
              placement="bottomLeft"
              @confirm="handleRemove(record.ID)"
            >
              <a-icon slot="icon" type="question-circle-o" />
              <a href="#">删除</a>
            </a-popconfirm>
          </template>
        </span>
      </s-table>
    </a-card>
    <edit-menu :visible.sync="visibles" :propsData="propsData"></edit-menu>
  </page-header-wrapper>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getMenuList, delMenu } from '@/api/system'
import EditMenu from './edit'
const columns = [
  {
    title: '菜单标题',
    align: 'left',
    dataIndex: 'Title',
    scopedSlots: { customRender: 'menuTitle' }
  },
  {
    title: '菜单类型',
    align: 'left',
    width: 150,
    scopedSlots: { customRender: 'menuType' }
  },
  {
    title: '图标',
    align: 'left',
    dataIndex: 'Icon',
    scopedSlots: { customRender: 'icon' },
    width: 150
  },
  {
    title: '菜单url',
    align: 'left',
    dataIndex: 'MenuUrl',
    width: 320
  },
  {
    title: '排序值',
    align: 'left',
    dataIndex: 'RankingValue',
    width: 150
  },
  {
    title: '操作',
    align: 'left',
    width: 220,
    fixed: 'right',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'MenuList',
  components: {
    STable,
    Ellipsis,
    EditMenu
  },
  data () {
    this.columns = columns
    return {
      visibles: false,
      // 查询参数
      queryParam: {
        Title: ''
      },
      propsData: {
        type: 'add',
        data: ''
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getMenuList(Object.assign({}, parameter, this.queryParam)).then(res => {
          return res
        })
      }
    }
  },
  methods: {
    handleMenu (type, data) {
      this.propsData = { type, data }
      this.visibles = true
    },
    handleRemove (id) {
      delMenu({ id }).then((res) => {
        if (res.code === 200) {
          this.$refs.table.refresh()
          this.$message.success('菜单删除成功', 2.5)
        } else {
          this.$message.error('菜单删除失败, 请稍后重试', 2.5)
        }
      }).catch((err) => {
        this.$message.error(err, 2.5)
      })
    },
    handleReset () {
      this.queryParam = {
        Title: ''
      }
      this.$refs.table.refresh()
    }
  }
}
</script>
<style lang="less" scoped>
  .menu-table {
    /deep/.ant-tag {
      cursor: pointer;
    }
  }
</style>
